﻿using SolidEdgeAssembly;
using SolidEdgeFramework;
using SolidEdgePart;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Web.UI;
using System.Windows.Forms;
//using TGCADDevUtils;
using TGCommunity.AddIn;

namespace TGComacAddin
{
    /// <summary>
    /// 天工CAD Ribbon 按钮-命令枚举
    /// </summary>
    public enum RibbonButton
    {
        /// <summary>
        /// 不绑定命令
        /// </summary>
        Cmd1 = 0,
        /// <summary>
        /// 绑定命令
        /// </summary>
        Cmd2 = 1,
        /// <summary>
        /// 默认空的
        /// </summary>
        None = 1000
    }

    /// <summary>
    /// 天工CAD Ribbon创建示例对象
    /// </summary>
    internal class TGCADRibbon : TGCommunity.AddIn.Ribbon
    {
        #region 成员变量
        /// <summary>
        /// xml资源路径
        /// </summary>
        private const string _strEmbeddedResourceName = "TGComacAddin.res.TGRibbonConfig.xml";//默认命名空间+文件资源夹名称+xml文件名称
        /// <summary>
        /// 未绑定命令按钮
        /// </summary>
        private RibbonControl _UnBandingButton { get; set; } = null;
        /// <summary>
        /// 绑定命令按钮
        /// </summary>
        private RibbonControl _BandingButton { get; set; } = null;
        #endregion 成员变量

        /// <summary>
        /// Ribbon构造函数
        /// </summary>
        public TGCADRibbon()
        {
            // Get a reference to the current assembly. This is where the ribbon XML is embedded.
            var assembly = System.Reflection.Assembly.GetExecutingAssembly();

            // In this example, XML file must have a build action of "Embedded Resource".
            // xml文件必须设置为嵌入的资源，否则获取xml的路径是会为null
            this.LoadXml(assembly, _strEmbeddedResourceName);

            _UnBandingButton = GetButton((int)RibbonButton.Cmd1);
            _BandingButton = GetButton((int)RibbonButton.Cmd2);
        }

        #region Ribbon按钮事件
        /// <summary>
        /// Ribbon controls event.
        /// </summary>
        /// <param name="control"></param>
        public override void OnControlClick(RibbonControl control)
        {
            var application = TGComacAddin.Instance.Application;  //TGCADDevSample1（类名）
            //Demonstrate how to handle commands without binding to a local variable.
            switch (control.CommandId)
            {
                case (int)RibbonButton.Cmd1:
                    UnBindingCommand(application);
                    break;
                case (int)RibbonButton.Cmd2:
                    BindingCommand(application);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 绑定命令按钮执行的事件的示例
        /// </summary>
        /// <param name="application"></param>
        /// <param name="control"></param>
        public void BindingCommand(SolidEdgeFramework.Application application)
        {
            try
            {
                TGUserWindow userWindow = new TGUserWindow();
                userWindow.Show();
                //userWindow.
                //TGCustomCommand customCommand = new TGCustomCommand(application);
                //customCommand.RunCommand(userWindow);
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }

        /// <summary>
        /// 不绑定命令按钮执行的事件的示例
        /// </summary>
        /// <param name="application"></param>
        /// <param name="control"></param>
        public void UnBindingCommand(SolidEdgeFramework.Application application)
        {
            //// Connect to or start Solid Edge.
            //application = TGCADDevUtils.TGTools.ConnectToSolidEdge(true);

            //// Make sure user can see the GUI.
            //application.Visible = true;

            //// Bring Solid Edge to the foreground.
            //application.Activate();

            //var doc = application.ActiveDocument;
            //if (doc is SolidEdgePart.PartDocument)
            //{
            //    // get current part document.
            //    SolidEdgePart.PartDocument partDocument = (SolidEdgePart.PartDocument)application.ActiveDocument;

            //    // Always a good idea to give SE a chance to breathe.
            //    application.DoIdle();

            //    // Create geometry.
            //    TGCADDevUtils.TGTools.CreateFiniteExtrudedProtrusion(partDocument);

            //    // Create holes.
            //    TGCADDevUtils.TGTools.CreateHoles(partDocument);

            //    // Switch to ISO view.
            //    application.StartCommand((SolidEdgeFramework.SolidEdgeCommandConstants)SolidEdgeConstants.PartCommandConstants.PartViewISOView);
            //}
        }
        #endregion Ribbon按钮事件
    }
}
